package com.company.cloud.art.modules.bus.consumer;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.company.cloud.art.modules.mgr.service.ActArtistMgrService;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.mq.redis.client.consumer.ISaasMqConsumer;
import com.company.cloud.common.mq.redis.client.consumer.MQConsumeMessage;
import com.company.cloud.common.mq.redis.client.consumer.MQSaasResult;
import com.company.cloud.common.mq.redis.client.consumer.annotation.MQRedisConsumer;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.common.server.config.mq.constant.MQTopicKeys;
import com.company.common.server.config.mq.models.MQPrdModel;
import com.company.common.server.constant.CloudConstant;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Set;


@Log4j2
@MQRedisConsumer(topic= MQTopicKeys.MQ_PRD_Change_Topic,group="art_prd",consumer = "art_prd_consumer")
@Service
public class ArtPrdRedisComsumer implements ISaasMqConsumer {

    @Autowired
    private ActArtistMgrService actArtistMgrService;

    @Override
    public MQSaasResult consume(MQConsumeMessage data) throws Exception {
        try {
            MQPrdModel prdModel=data.getContent(MQPrdModel.class);
            if(prdModel!=null){
                Set<String> artistNos= StrSplitUtils.parse2set(prdModel.getBizValue4(),String.class);
                if(!CollectionUtils.isEmpty(artistNos)){
                    if(CloudConstant.EntityOpWayEnum.ENTITY_ADD.getCode().equals(prdModel.getOpWay())){
                        actArtistMgrService.artistAddPrd(artistNos,prdModel.getProductNo());
                    }
                    if(CloudConstant.EntityOpWayEnum.ENTITY_EDIT.getCode().equals(prdModel.getOpWay())){
                        actArtistMgrService.artistEditPrd(artistNos,prdModel.getProductNo());
                    }
                    if(CloudConstant.EntityOpWayEnum.ENTITY_DEL.getCode().equals(prdModel.getOpWay())){
                        actArtistMgrService.artistDelPrd(artistNos,prdModel.getProductNo());
                    }
                }
            }
            return MQSaasResult.getSuccess(data.getId());
        }catch (Exception e){
            log.debug("+++++ ArtPrdComsumer Error : {}",e.getMessage());
            return MQSaasResult.getFail(data.getId());
        }
    }


}
